using System;
using System.Windows;

namespace Wintellect.Touch
{
    internal class MouseGestureAction : GestureAction
    {
        #region Fields
        private readonly FrameworkElement _mouseEventSourceItem;
        private Boolean _isMouseCaptured; 
        #endregion

        #region Constructor(s) and Initialization
        /// <summary>
        /// Initializes a new instance of the <see cref="MouseGestureAction"/> class.
        /// </summary>
        /// <param name="mouseEventSourceItem">The mouse event source item.</param>
        /// <param name="currentElement">The current element.</param>
        public MouseGestureAction(FrameworkElement mouseEventSourceItem, UIElement currentElement)
            : base(currentElement)
        {
            _mouseEventSourceItem = mouseEventSourceItem;

        } 
        #endregion

        #region Overrides

        /// <summary>
        /// Provides implementation-specific initialization
        /// </summary>
        /// <returns></returns>
        protected override bool OnInitialize()
        {
            _isMouseCaptured = _mouseEventSourceItem.CaptureMouse();
            return _isMouseCaptured;
        }

        /// <summary>
        /// Provides implementation-specific Release functionality
        /// </summary>
        protected override void OnRelease()
        {
            base.OnRelease();
            if (_isMouseCaptured)
            {
                _mouseEventSourceItem.ReleaseMouseCapture();
                _isMouseCaptured = false;
            }
        }

        /// <summary>
        /// Gets a value indicating whether this instance has been initialized.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance has been initialized; otherwise, <c>false</c>.
        /// </value>
        protected override Boolean HasBeenInitialized
        {
            get { return _isMouseCaptured; }
        }

        /// <summary>
        /// Gets the implementation-specific move-threshold.
        /// </summary>
        /// <value>The move threshold.</value>
        protected override Double MoveThreshold
        {
            get { return 5.0; } // smaller threshold for Mouse actions
        } 
        #endregion
    }
}